package com.dc.free.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import com.jfinal.core.Const;
import com.jfinal.kit.StrKit;

/**
 * response格式    data数据可以用MapUtil生成，
 * @author quan
 *
 */
public class StatusUtil {
	
	private Map<String, Object> status = new HashMap<String, Object>();
	private Map<String, Object> data = null;
	private static final Map<String, String> codeMsg = loadCodeMsg();
	
	/**
	 * 加载状态码属性文件
	 * @return
	 */
	private static Map<String, String> loadCodeMsg() {
		InputStream inputStream =Thread.currentThread().getContextClassLoader().getResourceAsStream("status.properties");
		Map<String, String> map = new HashMap<String, String>();
		Properties properties = new Properties();
		
		if(inputStream != null) {
			try {
				properties.load(new InputStreamReader(inputStream, Const.DEFAULT_ENCODING));
				for(String key: properties.stringPropertyNames()) {
					map.put(key, properties.getProperty(key));
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return map;
	}
	
	/**
     * 通过返回码获取返回信息
     * @param errCode 错误码
     * @return {String}
     */
    public static String getMsg(String code){
        String result = codeMsg.get(code);
        return result != null ? result : "未知返回码：" + code;
    }
	
	public static StatusUtil create() {
		return new StatusUtil();
	}
	
	public static StatusUtil create(String key, String value) {
		return new StatusUtil(key, value);
	}
	
	public static StatusUtil create(String code, String stack, Object _data) {
		return create(code, null, stack, _data);
	}
	
	public static StatusUtil create(String code, String msg, String stack, Object _data) {
		StatusUtil su = new StatusUtil();
		String codeMsg = getMsg(code);
		
		su.putStatus("code", code+"")
		  .putStatus("msg", StrKit.notBlank(msg) ? msg : codeMsg)
		  .putStatus("stack", stack)
		  .putStatus("data", su.data != null ? su.data : _data);
		return su;
	}
	
	public StatusUtil() {}
	
	public StatusUtil(String key, Object value) {
		status.put(key, value);
	}
	
	public StatusUtil putStatus(String key, Object value) {
		status.put(key, value);
		return this;
	}
	
	public StatusUtil putData(String key, String value) {
		if(data == null) data = new HashMap<String, Object>();
		
		data.put(key, value);
		return this;
	}
	
	public StatusUtil putDate(Map<String, Object> data) {
		if(data == null) this.data = new HashMap<String, Object>();
		
		data.putAll(data);
		return this;
	}
	
	public Map<String, Object> getStatus(String code, String msg, String stack, Object _data, Object... args) {
		msg = StrKit.isBlank(msg) ? getMsg(code) : msg;
		msg = String.format(msg, args);
		status.put("code", code);
		status.put("msg", msg);
		status.put("stack", stack);
		status.put("data", data != null ? data : _data);
		
		return status;
	}
	
	/*public Map<String, Object> getStatus(String code, String stack, Object data) {
		return getStatus(code, null, stack, data);
	}
	
	public Map<String, Object> getStatus(String stack, Object data) {
		return getStatus("0", null, stack, data);
	}
	
	public Map<String, Object> getStatus(Object data) {
		return getStatus("0", null, null, data);
	}
	
	public Map<String, Object> getStatus() {
		return status;
	}*/
	
}
